﻿#include "newprojectsettings.h"

NewProjectSettings::NewProjectSettings(QWidget *parent) :
    QWidget(parent)
{
    this->setWindowTitle(tr("项目配置"));
    this->setObjectName("NewProjectSettings");
    this->setWindowModality(Qt::ApplicationModal);
    this->manualCreateUI();
    this->resize(QGuiApplication::primaryScreen()->geometry().width()/10*3,this->minimumHeight());
}

NewProjectSettings::~NewProjectSettings()
{
}

void NewProjectSettings::manualCreateUI()
{
    QLabel *pnlb = new QLabel(tr("项目名称"));
    QLabel *pllb = new QLabel(tr("保存位置"));
    QLabel *splb = new QLabel(tr("源码目录"));

    projectName = new QLineEdit();
    projectLocationPath = new QLineEdit();
    sourceLocationPath = new QLineEdit();

    QPushButton *ok = new QPushButton(tr("确定"));
    connect(ok,SIGNAL(clicked()),this,SLOT(slotOk()));
    QPushButton *prjPathBtn = new QPushButton(tr("..."));
    connect(prjPathBtn,SIGNAL(clicked()),this,SLOT(slotPrjPath()));
    QPushButton *srcPathBtn = new QPushButton(tr("..."));
    connect(srcPathBtn,SIGNAL(clicked()),this,SLOT(slotSrcPath()));


    QGridLayout *top = new QGridLayout();
    top->addWidget(pnlb,0,0);
    top->addWidget(projectName,0,1);
    top->addWidget(ok,0,2);
    top->addWidget(pllb,1,0);
    top->addWidget(projectLocationPath,1,1);
    top->addWidget(prjPathBtn,1,2);
    top->addWidget(splb,2,0);
    top->addWidget(sourceLocationPath,2,1);
    top->addWidget(srcPathBtn,2,2);
    top->setColumnStretch(1,1);

    setLayout(top);
}

void NewProjectSettings::slotOk()
{

}

void NewProjectSettings::slotPrjPath()
{
#if QT_MAJOR_VERSION == 4
    QString docPath = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
#else
    QString docPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#endif
    if(docPath.isEmpty())
        docPath.append(".");
    this->projectLocationPath->setText(QFileDialog::getExistingDirectory(this, tr("保存位置"),\
                                                                 docPath,\
            QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks));
}

void NewProjectSettings::slotSrcPath()
{
#if QT_MAJOR_VERSION == 4
    QString docPath = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
#else
    QString docPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#endif
    if(docPath.isEmpty())
        docPath.append(".");
    this->sourceLocationPath->setText(QFileDialog::getExistingDirectory(this, tr("源码目录"),\
                                                                docPath,\
            QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks));
}

void NewProjectSettings::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_Escape:
        this->close();
        break;

    default:
        QWidget::keyPressEvent(event);
        break;
    }
}
